package com.zja.servlet;


import com.zja.model.JsonResponse;
import com.zja.model.User;
import com.zja.service.Impl.UserServiceImpl;
import com.zja.service.UserService;
import com.zja.util.ServletUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
/**
 * @author JA
 * @date: 2025/5/5
 * description:修改用户密码
 */
@WebServlet("/api/user/updatePassword")
public class UpdateUserPasswordServlet extends HttpServlet {
    private final UserService userService = new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User input = ServletUtil.readJsonBody(req, User.class);
        if (input.getPassword() == null || input.getPassword().isEmpty()) {
            ServletUtil.writeJsonResponse(resp, JsonResponse.error("密码不能为空"));
            return;
        }

        boolean success = userService.updatePassword(input.getId(), input.getPassword());
        ServletUtil.writeJsonResponse(resp, success ? JsonResponse.success("密码修改成功") : JsonResponse.error("修改失败"));
    }
}
